package com.cly.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Configuration
public class Oauth2FeignInterceptor implements RequestInterceptor {


    @Override
    public void apply(RequestTemplate template) {
        //怎么拿到之前的token
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (!ObjectUtils.isEmpty(requestAttributes)){
            //说明是有请求的
            //这个request是之前的request 这里面有token
            HttpServletRequest request = requestAttributes.getRequest();
            //拿到token bearer
            String authorization = request.getHeader("Authorization");
            if (StringUtils.hasText(authorization)){
                //传递到下一个request对象里面去
                template.header("Authorization",authorization);
                return;
            }
            template.header("Authorization","eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6WyJyZWFkIl0sImV4cCI6Mzc2NDU5Mjc1NiwianRpIjoiODIwYzc0YjYtZDVkOC00MzA5LTliMmEtMzExMzgzZTE4Mjk4IiwiY2xpZW50X2lkIjoiY2xpZW50In0.IiojtylWp9vv8CRre0TY3ZI24V1bmG8xkoI7W2KuVe4gGYNnwezuMinvuK3PEv_nq1V9IPSmq9GN1iiyNo40NcJkG4ScJGto8NUk_vi7m9tbbndnTf6iiBii_AJ5gdvAyOXrXU5kITgOPsKvc72MiJ5uTWJLckhMo_8_tPjotdQ2p6QbEa9wZUkifx0dtsi27UCeLbUw_kOxkGwWUVLY-iUFKO2KVVscd07swNeKPieMHUaB9SHKLBEtibF-ZIMPmZfYh7r6UA3kmFlLL6_NtljFyE1EPZ8zr084r7-oqHsnwtQrEQ9KT78MMrbWt3oztwAeiron8gBwFrCHGMoABA");
        }
    }
}
